package cn.miwu52.homeworkerp.Controller;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.miwu52.homeworkerp.R;
import cn.miwu52.homeworkerp.dao.UserDao;

public class MainActivity extends AppCompatActivity {
    private Button button_login;
    private EditText userName;
    private EditText passWord;
    private CheckBox rememberPassWord;
    private CheckBox autoLogin;
    private SharedPreferences sharedPreferences;
    private UserDao userDao;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //sharedPreferences = getPreferences(Activity.MODE_PRIVATE);
        if (sharedPreferences.getBoolean("loginStatus",false)){
            Intent intent = new Intent(MainActivity.this,Menu.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        userDao = new UserDao(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_login = findViewById(R.id.Button_Login);
        userName = findViewById(R.id.userName);
        passWord = findViewById(R.id.passWord);
        rememberPassWord = findViewById(R.id.rememberPassWord);
        autoLogin = findViewById(R.id.autoLogin);

        sharedPreferences = getPreferences(Activity.MODE_PRIVATE);

        userName.setText(sharedPreferences.getString("userName",""));
        passWord.setText(sharedPreferences.getString("passWord",""));

        rememberPassWord.setChecked(sharedPreferences.getBoolean("rememberPassWord",false));
        autoLogin.setChecked(sharedPreferences.getBoolean("autoLogin",false));
        TextView reg = findViewById(R.id.reg);
        TextView forgot = findViewById(R.id.forgot);


        if (autoLogin.isChecked()){
            if (userName.getText().toString().equals("")||passWord.getText().toString().equals("")){
                Toast toast = Toast.makeText(getApplicationContext(),"账号或密码不得为空",Toast.LENGTH_LONG);
                toast.show();
                return;
            }else if (userDao.authByUserNameAndPassWord(userName.getText().toString(),passWord.getText().toString())){
                Toast toast = Toast.makeText(getApplicationContext(),"恭喜您，登录成功",Toast.LENGTH_SHORT);
                toast.show();
                sharedPreferences.edit()
                        .putBoolean("loginStatus", true)
                        .putString("userName", userName.getText().toString())
                        .commit();
                Intent intent = new Intent(MainActivity.this,Menu.class);
                startActivity(intent);
                finish();
            }else {
                Toast toast = Toast.makeText(getApplicationContext(),"登录失败，账号或密码错误",Toast.LENGTH_SHORT);
                toast.show();
            }
        }

        //输入密码捂住眼
        passWord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                ImageView login_right = findViewById(R.id.login_right);
                ImageView login_left = findViewById(R.id.login_left);
                if (b){
                    login_right.setImageResource(R.drawable.login_22_close);
                    login_left.setImageResource(R.drawable.login_33_close);
                }else{
                    login_right.setImageResource(R.drawable.login_22_open);
                    login_left.setImageResource(R.drawable.login_33_open);
                }
            }
        });
        //登录逻辑
        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (rememberPassWord.isChecked()){
                    sharedPreferences.edit()
                            .putString("userName",userName.getText().toString())
                            .putString("passWord",passWord.getText().toString())
                            .putBoolean("rememberPassWord",true)
                            .commit();
                }
                else{
                    sharedPreferences.edit()
                            .putString("userName", userName.getText().toString())
                            .putString("passWord",null)
                            .putBoolean("rememberPassWord",false)
                            .putBoolean("autoLogin",false)
                            .commit();
                }
                if (autoLogin.isChecked()){
                    sharedPreferences.edit()
                            .putBoolean("autoLogin",true)
                            .commit();
                }

                if (userName.getText().toString().equals("")||passWord.getText().toString().equals("")){
                    Toast toast = Toast.makeText(getApplicationContext(),"账号或密码不得为空",Toast.LENGTH_LONG);
                    toast.show();
                    return;
                }else if (userDao.authByUserNameAndPassWord(userName.getText().toString(),passWord.getText().toString())){
                    Toast toast = Toast.makeText(getApplicationContext(),"恭喜您，登录成功",Toast.LENGTH_SHORT);
                    toast.show();
                    sharedPreferences.edit()
                            .putBoolean("loginStatus",true)
                            .commit();
                    Intent intent = new Intent(MainActivity.this,Menu.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast toast = Toast.makeText(getApplicationContext(),"登录失败，账号或密码错误",Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
        //记住密码 自动登录 要求勾选自动登录时强制勾选记住密码 已实现相关业务逻辑
        rememberPassWord.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b){
                    autoLogin.setChecked(false);
                }
            }
        });
        autoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    rememberPassWord.setChecked(true);
                }
            }
        });
        //注册 或 忘记密码
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Register.class);
                startActivity(intent);
                finish();
            }
        });
        forgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog error = new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.erp)
                        .setTitle("error")
                        .setMessage("对不起，您已违反账号管理规定【使用者应牢记密码且保证密码免泄露】，请重新注册用户使用")
                        .create();
                error.show();
            }
        });

    }
}